package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.device.model.entity.DeviceWorkmanship;
import com.wing.device.model.request.DeviceWorkmanshipForm;
import com.wing.device.model.response.DeviceWorkmanshipVO;
import com.wing.product.dao.ProductWorkmanshipDao;
import com.wing.product.model.entity.Product;
import com.wing.product.model.entity.ProductCategory;
import com.wing.product.model.entity.ProductWorkmanship;
import com.wing.product.model.request.ProductWorkmanshipForm;
import com.wing.product.model.response.ProductWorkmanshipVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 产品生产工艺分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProductWorkmanshipServiceImpl extends ServiceImpl<ProductWorkmanshipDao, ProductWorkmanship> implements ProductWorkmanshipService {

    private static long ROOT_ID=-1l;

    @Override
    public int add(ProductWorkmanshipForm productWorkmanshipForm) {

        //校验

        ProductWorkmanship productWorkmanship =new ProductWorkmanship();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProductWorkmanship.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(productWorkmanshipForm, productWorkmanship, copyOptions);
        if (productWorkmanshipForm.getParentId()!=ROOT_ID){
            ProductWorkmanship productWorkmanshipParent=new ProductWorkmanship();
            productWorkmanshipParent.setId(productWorkmanshipForm.getParentId());
            productWorkmanshipParent.setHasChild(true);
            if (productWorkmanshipForm.getIsDeleted()){
                productWorkmanshipParent.setHasEnableChild(true);
            }
            baseMapper.updateById(productWorkmanshipParent);
        }
        return baseMapper.insert(productWorkmanship);
    }

    @Override
    public List<ProductWorkmanshipVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProductWorkmanshipVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProductWorkmanshipVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProductWorkmanshipVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int del(Long id) {
        ProductWorkmanshipVO productWorkmanshipVO=baseMapper.get4appById(id);
        if (productWorkmanshipVO!=null){
            int delCount=baseMapper.deleteById(id);
            if (productWorkmanshipVO.getParentId()!=ROOT_ID){
                int count=baseMapper.selectCount(new QueryWrapper<ProductWorkmanship>().eq("parent_id",productWorkmanshipVO.getParentId()));
                if (count==0){
                    ProductWorkmanship productWorkmanshipParent=new ProductWorkmanship();
                    productWorkmanshipParent.setId(productWorkmanshipVO.getParentId());
                    productWorkmanshipParent.setHasChild(false);
                    productWorkmanshipParent.setHasEnableChild(false);
                    baseMapper.updateById(productWorkmanshipParent);
                }
            }

            return delCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int enable(Long id) {
        ProductWorkmanshipVO productWorkmanshipVO=baseMapper.get4appById(id);
        if (productWorkmanshipVO!=null){
            ProductWorkmanship productWorkmanship=new ProductWorkmanship();
            productWorkmanship.setId(id);
            productWorkmanship.setIsDeleted(true);
            int updateCount=baseMapper.updateById(productWorkmanship);
            if (productWorkmanshipVO.getParentId()!=ROOT_ID){
                ProductWorkmanship productCategoryParent=new ProductWorkmanship();
                productCategoryParent.setId(productWorkmanshipVO.getParentId());
                productCategoryParent.setHasEnableChild(true);
                baseMapper.updateById(productCategoryParent);
            }

            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        ProductWorkmanshipVO productWorkmanshipVO=baseMapper.get4appById(id);
        if (productWorkmanshipVO!=null){
            ProductWorkmanship deviceWorkmanship=new ProductWorkmanship();
            deviceWorkmanship.setId(id);
            deviceWorkmanship.setIsDeleted(false);
            int updateCount=baseMapper.updateById(deviceWorkmanship);
            if (productWorkmanshipVO.getParentId()!=ROOT_ID){
                int count=baseMapper.selectCount(new QueryWrapper<ProductWorkmanship>().eq("parent_id",productWorkmanshipVO.getParentId()).eq("is_deleted",true));
                if (count==0){
                    ProductWorkmanship productWorkshipParent=new ProductWorkmanship();
                    productWorkshipParent.setId(productWorkmanshipVO.getParentId());
                    productWorkshipParent.setHasEnableChild(false);
                    baseMapper.updateById(productWorkshipParent);
                }
            }

            return updateCount;
        }

        return 0;
    }

    @Override
    @Transactional
    public int update(ProductWorkmanshipForm productWorkmanshipForm) {
        ProductWorkmanshipVO productWorkmanshipVO=baseMapper.get4appById(productWorkmanshipForm.getId());
        if (productWorkmanshipVO!=null){
            if (!productWorkmanshipVO.getParentId().equals(productWorkmanshipForm.getParentId())){
                boolean isUpdateEnable=productWorkmanshipVO.getIsDeleted().equals(productWorkmanshipForm.getIsDeleted());
                ProductWorkmanship productWorkmanship=new ProductWorkmanship();
                BeanUtils.copyProperties(productWorkmanshipForm,productWorkmanship);
                baseMapper.updateById(productWorkmanship);

                //update new parent category
                if (productWorkmanshipForm.getParentId()!=ROOT_ID){
                    ProductWorkmanship parentProductWorkmanship=new ProductWorkmanship();
                    parentProductWorkmanship.setId(productWorkmanshipForm.getParentId());
                    parentProductWorkmanship.setHasChild(true);
                    if (productWorkmanshipForm.getIsDeleted()){
                        parentProductWorkmanship.setHasEnableChild(true);
                    }
                    baseMapper.updateById(parentProductWorkmanship);
                }


                //update current db product category
                if (productWorkmanshipVO.getParentId()!=ROOT_ID){
                    int count=baseMapper.selectCount(new QueryWrapper<ProductWorkmanship>().eq("parent_id",productWorkmanshipVO.getParentId()));
                    if (count==0){
                        ProductWorkmanship productWorkmanshipParent=new ProductWorkmanship();
                        productWorkmanshipParent.setId(productWorkmanshipVO.getParentId());
                        productWorkmanshipParent.setHasChild(false);
                        baseMapper.updateById(productWorkmanshipParent);
                    }
//                if (isUpdateEnable&&!deviceCategoryForm.getIsDeleted()){
                    count=baseMapper.selectCount(new QueryWrapper<ProductWorkmanship>().eq("parent_id",productWorkmanshipVO.getParentId()).eq("is_deleted",true));
                    if (count==0){
                        ProductWorkmanship productWorkmanshipParent=new ProductWorkmanship();
                        productWorkmanshipParent.setId(productWorkmanshipVO.getParentId());
                        productWorkmanshipParent.setHasEnableChild(false);
                        baseMapper.updateById(productWorkmanshipParent);
                    }
                    //                }
                }


            }else{
                boolean isUpdateEnable=productWorkmanshipVO.getIsDeleted().equals(productWorkmanshipForm.getIsDeleted());
                ProductWorkmanship deviceWorkmanship=new ProductWorkmanship();
                BeanUtils.copyProperties(productWorkmanshipForm,deviceWorkmanship);
                baseMapper.updateById(deviceWorkmanship);
                if (isUpdateEnable&&!productWorkmanshipForm.getIsDeleted()&&productWorkmanshipForm.getParentId()!=ROOT_ID){
                    int count=baseMapper.selectCount(new QueryWrapper<ProductWorkmanship>().eq("parent_id",productWorkmanshipVO.getParentId()).eq("is_deleted",true));
                    if (count==0){
                        ProductWorkmanship productWorkmanshipParent=new ProductWorkmanship();
                        productWorkmanshipParent.setId(productWorkmanshipVO.getParentId());
                        productWorkmanshipParent.setHasEnableChild(false);
                        baseMapper.updateById(productWorkmanshipParent);
                    }
                }

            }
        }
        return 0;
    }
}
